﻿#region The MIT License (MIT)
// The MIT License (MIT)
// Copyright (c) 2010 - 2013 Sovacode LLC
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace Sovacode.RubiksCube.Common.View
{
    /// <summary>
    /// Class WindowCloseBehavior
    /// </summary>
    public class WindowCloseBehavior
    {
        /// <summary>
        /// Gets the closed.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>ICommand.</returns>
        public static ICommand GetClosed(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(ClosedProperty);
        }

        /// <summary>
        /// Sets the closed.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="value">The value.</param>
        public static void SetClosed(DependencyObject obj, ICommand value)
        {
            obj.SetValue(ClosedProperty, value);
        }

        /// <summary>
        /// The closed property
        /// </summary>
        public static readonly DependencyProperty ClosedProperty
            = DependencyProperty.RegisterAttached(
            "Closed", typeof(ICommand), typeof(WindowCloseBehavior),
            new UIPropertyMetadata(ClosedChanged));

        /// <summary>
        /// Closed the changed.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void ClosedChanged(
            DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            var window = target as Window;

            if (window == null)
            {
                return;
            }

            if (e.NewValue != null)
            {
                window.Closed += WindowClosed;
            }
            else
            {
                window.Closed -= WindowClosed;
            }
        }

        /// <summary>
        /// Gets the closing.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>ICommand.</returns>
        public static ICommand GetClosing(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(ClosingProperty);
        }

        /// <summary>
        /// Sets the closing.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="value">The value.</param>
        public static void SetClosing(DependencyObject obj, ICommand value)
        {
            obj.SetValue(ClosingProperty, value);
        }

        /// <summary>
        /// The closing property
        /// </summary>
        public static readonly DependencyProperty ClosingProperty
            = DependencyProperty.RegisterAttached(
            "Closing", typeof(ICommand), typeof(WindowCloseBehavior),
            new UIPropertyMetadata(ClosingChanged));

        /// <summary>
        /// Closings the changed.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void ClosingChanged(
            DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            var window = target as Window;

            if (window == null)
            {
                return;
            }

            if (e.NewValue != null)
            {
                window.Closing += WindowClosing;
            }
            else
            {
                window.Closing -= WindowClosing;
            }
        }

        /// <summary>
        /// Gets the cancel closing.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>ICommand.</returns>
        public static ICommand GetCancelClosing(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CancelClosingProperty);
        }

        /// <summary>
        /// Sets the cancel closing.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="value">The value.</param>
        public static void SetCancelClosing(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CancelClosingProperty, value);
        }

        /// <summary>
        /// The cancel closing property
        /// </summary>
        public static readonly DependencyProperty CancelClosingProperty
            = DependencyProperty.RegisterAttached(
            "CancelClosing", typeof(ICommand), typeof(WindowCloseBehavior));

        /// <summary>
        /// Windows the closed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        static void WindowClosed(object sender, EventArgs e)
        {
            ICommand closed = GetClosed(sender as Window);
            
            if (closed != null)
            {
                closed.Execute(null);
            }
        }

        /// <summary>
        /// Windows the closing.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="CancelEventArgs"/> instance containing the event data.</param>
        private static void WindowClosing(object sender, CancelEventArgs e)
        {
            ICommand closing = GetClosing(sender as Window);

            if (closing == null)
            {
                return;
            }

            if (closing.CanExecute(null))
            {
                closing.Execute(null);
            }
            else
            {
                ICommand cancelClosing = GetCancelClosing(sender as Window);

                if (cancelClosing == null)
                {
                    return;
                }

                cancelClosing.Execute(null);

                e.Cancel = true;
            }
        }
    }
}